/*
 * Copyright (c) 2006-2008 Reliable Source, Inc and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Mozilla Public License v.1.1
 * and BSD licenses.
 * 
 * http://www.eclipse.org/legal/epl-v10.html
 * http://www.opensource.org/licenses/bsd-license.php
 * http://www.mozilla.org/MPL/MPL-1.1.html
 *
 * Contributors:
 *      IBM Corporation - initial API and implementation
 *      patientos.org - domain and database persistence layer              
 *      Reliable Source, Inc. - user interface client platform
 *
 */
package org.reliablesource.client.ui.workbench.views;

import java.util.ArrayList;
import java.util.Iterator;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.part.ViewPart;
import org.reliablesource.client.ui.viewers.IReliableViewer;

/**
 * 
 * ReliableViewPart.java<br>
 * Created on: May 12, 2007<br>
 *
 * @author <a href="mailto:dennis.park@gmail.com">Dennis Park</a>
 *
 * @category
 * @since 1.0
 */
abstract public class ReliableViewPart extends ViewPart implements
	IReliableViewPart {

    protected IReliableViewer viewer;
    protected ArrayList<Action> viewActions;

    @Override
    public void createPartControl(Composite _parent) {
	viewer = createViewer();
	viewer.init();
	viewer.createViewerControl(_parent);
	viewer.addOpenListener(new ReliableOpenListener());
	viewActions = new ArrayList<Action>();
	contributeToActionBars();
    }

    /*
     * (non-Javadoc)
     * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
     */
    @Override
    public void setFocus() {
	//	    WorkbenchActivator.getDefault().getContextService().activateContext("part");
	if (null != viewer)
	    if (null != viewer.getControl()) {
		viewer.getControl().setFocus();
		//				WorkbenchActivator.getDefault().getContextService().activateContext("viewer");
	    }
    }

    /*
     * (non-Javadoc)
     * @see org.reliablesource.client.ui.workbench.views.IReliableViewPart#getViewer()
     */
    public IReliableViewer getViewer() {
	return viewer;
    }

    /**
     * @return
     * @category lifecycle
     */
    abstract protected IReliableViewer createViewer();

    /**
     * @category lifecycle
     */
    protected void setInput(final Object _input) {
	viewer.setInput(_input);
    }

    private void contributeToActionBars() {
	IActionBars bars = getViewSite().getActionBars();
	fillLocalPullDown(bars.getMenuManager());
	fillLocalToolBar(bars.getToolBarManager());

    }

    private void fillLocalPullDown(IMenuManager _manager) {

	Iterator<Action> _actions = viewActions.iterator();
	while (_actions.hasNext()) {
	    _manager.add(_actions.next());
	    _manager.add(new Separator());
	}
    }

    private void fillLocalToolBar(IToolBarManager _manager) {
	Iterator<Action> _actions = viewActions.iterator();
	while (_actions.hasNext()) {
	    _manager.add(_actions.next());
	    _manager.add(new Separator());
	}
    }

    protected void makeActions(){
	// create actions 
    }
}
